﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Table;
using System.Text;

namespace ERPTemplate
{
    public class MessageBox
    {
        /// <summary>
        /// 提示
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="control"></param>
        public static void Alert(string msg, Control control)
        {
            //            Page page  = (Page)control;
            //            HtmlLink myCSS = new HtmlLink();



            //myCSS.Href = "/CSS/jquery.alerts.css";
            //myCSS.Attributes.Add("rel", "stylesheet");
            //myCSS.Attributes.Add("type", "text/css");
            //            myCSS.Attributes.Add("media","screen");
            //page.Header.Controls.Add(myCSS);

            ////JavaScript!
            //HtmlGenericControl myJavaScript = new HtmlGenericControl();
            //myJavaScript.TagName = "script";
            //myJavaScript.Attributes.Add("type", "text/javascript");
            //            myJavaScript.Attributes.Add("src","/JS/jquery-1.3.2.min.js");
            //            page.Header.Controls.Add(myJavaScript);

            //            HtmlGenericControl myJavaScript1 = new HtmlGenericControl();
            //            myJavaScript1.TagName = "script";
            //myJavaScript1.Attributes.Add("type", "text/javascript");
            //            myJavaScript1.Attributes.Add("src","/JS/jquery.alerts.js");

            //            page.Header.Controls.Add(myJavaScript1);



            Show(control, msg);
            //RegisterScript("  jAlert('" + msg + "','提示');", control); 
        }
        ///// <summary>
        ///// 显示消息提示对话框
        ///// </summary>
        ///// <param name="page">当前页面指针，一般为this</param>
        ///// <param name="msg">提示信息</param>
        //public static void Show(System.Web.UI.Control control, string msg)
        //{
        //    ScriptManager.RegisterStartupScript(control, control.GetType(), "message", "<script type =\"text/javascript\" >alert('" + msg.ToString() + "');</script>", false);
        //}
        //public static void Show(System.Web.UI.UpdatePanel updatePanel, string msg)
        //{
        //    ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), "HelloWorld", "alert('" + msg + "');", true);
        //    //page.ClientScript.RegisterStartupScript(updatePanel.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        //}
        /// <summary>
        /// 注册javascript脚本
        /// </summary>
        /// <param name="script">脚本源码</param>
        /// <param name="control">控件</param>
        public static void RegisterScript(string script, Control control)
        {
            ScriptManager.RegisterStartupScript(control, control.GetType(), "script" + (new DateTime()).Ticks.ToString(), "<script type=\"text/javascript\">" + script + "</script>", false);
        }

        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Control control, string msg)
        {
            ScriptManager.RegisterStartupScript(control, control.GetType(), "message", "<script type =\"text/javascript\" >alert('" + msg.ToString() + "');</script>", false);
        }
        public static void Show(System.Web.UI.UpdatePanel updatePanel, string msg)
        {
            ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), "HelloWorld", "alert('" + msg + "');", true);
            //page.ClientScript.RegisterStartupScript(updatePanel.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }


        /// <summary>
        /// 提示并且跳转
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转url(页面的地址，最好是绝对地址)</param>
        /// <param name="control">注册控件(Page)</param>
        public static void ShowAndRedirect(string msg, string url, Control control)
        {
            RegisterScript("alert('" + msg + "');window.location='" + url + "';", control);
        }
        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());

        }



    }
}
